#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <stack>
#include <bitset>
#define F first
#define S second
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=2e5+10;
ll a[N];
void solve()
{
	ll n,d,m;
	cin>>n>>m>>d;
	ll cnt=1;
	for(int i=1;i<=n;i++) cin>>a[i];
	priority_queue<ll,vector<ll>,greater<ll> > q;
	ll sum=0;
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>0)
		{
			q.push(a[i]);
			sum+=a[i];
			if(q.size()>m)
			{
//				cout<<i<<endl;
				ll num=q.top();
				
//				cout<<num<<endl;
				q.pop();
				sum-=num;
//				cout<<i<<' '<<num<<' '<<sum-d*i<<endl;
			}
			ans=max(ans,sum-d*i);
		}
	}
	cout<<ans<<endl;
}
int main()
{
	IOS;
	int T=1;
	cin>>T;

	while(T--)
	{
		solve();
	}

	return 0;
}

